

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Moe Wang">
  <meta name="keywords" content="">
  
    <meta name="description" content="逻辑运算符扩展&amp;&amp; : 如果左边返回真，那么就返回右边的值，如果左边返回假，就返回左边的值  (短路运算)  123console.log( 0 &amp;&amp; 2 );console.log( 1&lt;2 &amp;&amp; 5&gt;4 );   1&lt;2 -&gt; true  公式整体得到 5&gt;4 -&gt; trueconsole.log( 1&gt;">
<meta property="og:type" content="article">
<meta property="og:title" content="初入JS（三）">
<meta property="og:url" content="https://moewang0321.github.io/2019/08/06/2019-08-06-%E5%88%9D%E5%85%A5js%EF%BC%88%E4%B8%89%EF%BC%89/index.html">
<meta property="og:site_name" content="moe_大杂烩">
<meta property="og:description" content="逻辑运算符扩展&amp;&amp; : 如果左边返回真，那么就返回右边的值，如果左边返回假，就返回左边的值  (短路运算)  123console.log( 0 &amp;&amp; 2 );console.log( 1&lt;2 &amp;&amp; 5&gt;4 );   1&lt;2 -&gt; true  公式整体得到 5&gt;4 -&gt; trueconsole.log( 1&gt;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://moewang0321.github.io/images/break%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF.png">
<meta property="og:image" content="https://moewang0321.github.io/images/break%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF_2.png">
<meta property="og:image" content="https://moewang0321.github.io/images/continue%E8%B7%B3%E5%87%BA.png">
<meta property="og:image" content="https://moewang0321.github.io/images/break%E8%B7%B3%E5%87%BA%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF.png">
<meta property="article:published_time" content="2019-08-06T12:01:55.000Z">
<meta property="article:modified_time" content="2023-02-14T08:02:46.723Z">
<meta property="article:author" content="Moe Wang">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://moewang0321.github.io/images/break%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF.png">
  
  
  
  <title>初入JS（三） - moe_大杂烩</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"moewang0321.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>moe_大杂烩</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="初入JS（三）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2019-08-06 20:01" pubdate>
          2019年8月6日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.8k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          24 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">初入JS（三）</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="逻辑运算符扩展"><a href="#逻辑运算符扩展" class="headerlink" title="逻辑运算符扩展"></a>逻辑运算符扩展</h2><p><strong>&amp;&amp; : 如果左边返回真，那么就返回右边的值，如果左边返回假，就返回左边的值  (短路运算)</strong> </p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">console</span>.log( <span class="hljs-number">0</span> &amp;&amp; <span class="hljs-number">2</span> );<br><span class="hljs-attribute">console</span>.log( <span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span> &amp;&amp; <span class="hljs-number">5</span>&gt;<span class="hljs-number">4</span> );   <span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span> -&gt; true  公式整体得到 <span class="hljs-number">5</span>&gt;<span class="hljs-number">4</span> -&gt; true<br><span class="hljs-attribute">console</span>.log( <span class="hljs-number">1</span>&gt;<span class="hljs-number">2</span> &amp;&amp; <span class="hljs-number">5</span>&gt;<span class="hljs-number">4</span> );    <span class="hljs-number">1</span>&gt;<span class="hljs-number">2</span> -&gt; false 公式整体得到  <span class="hljs-number">1</span>&gt;<span class="hljs-number">2</span> -&gt; false<br></code></pre></td></tr></table></figure>

<p><strong>|| : 如果左边返回真，那么就返回左边的值，如果左边返回假，就返回右边的值  (短路运算)</strong> </p>
<figure class="highlight livescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs livescript"><span class="hljs-regexp">//console.log( 0 || 2 );</span><br><span class="hljs-regexp"></span><br><span class="hljs-regexp">//</span>console.log( <span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span> || <span class="hljs-number">5</span>&lt;<span class="hljs-number">4</span> );   <span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span> -&gt; <span class="hljs-literal">true</span>  公式整体得到    <span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span>  -&gt;  <span class="hljs-literal">true</span><br></code></pre></td></tr></table></figure>

<p><strong>&amp; | 位运算 ，跟正常的&amp;&amp; || 功能类似 ，只不过是针对二进制的</strong></p>
<h2 id="JS控制流程"><a href="#JS控制流程" class="headerlink" title="JS控制流程"></a>JS控制流程</h2><h3 id="顺序结构"><a href="#顺序结构" class="headerlink" title="顺序结构"></a>顺序结构</h3><p>&emsp;&emsp;顺序结构就是程序从上到下、从左到右一句一句的去执行，没啥好说的。  </p>
<h3 id="选择结构"><a href="#选择结构" class="headerlink" title="选择结构"></a>选择结构</h3><h4 id="emsp-1-if判断"><a href="#emsp-1-if判断" class="headerlink" title="&emsp; 1.if判断"></a>&emsp; 1.if判断</h4><ol>
<li>单向选择 <figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs arcade">   <span class="hljs-keyword">if</span>( <span class="hljs-number">5</span> &gt; <span class="hljs-number">3</span> )&#123;    <span class="hljs-comment">// 单向选择</span><br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;if执行&#x27;</span>);<br>&#125; <br></code></pre></td></tr></table></figure></li>
<li>双向选择 <figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">if</span>( <span class="hljs-number">5</span> &lt; <span class="hljs-number">3</span> )&#123;    <span class="hljs-comment">//双向选择</span><br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;if执行&#x27;</span>);<br>  &#125;   <br>  <span class="hljs-keyword">else</span>&#123;<br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;else执行&#x27;</span>);<br>  &#125;<br></code></pre></td></tr></table></figure></li>
<li>多向选择 <figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">var</span> <span class="hljs-built_in">day</span> = <span class="hljs-string">&#x27;星期日&#x27;</span>;<br><br>  <span class="hljs-keyword">if</span>( <span class="hljs-built_in">day</span> == <span class="hljs-string">&#x27;星期一&#x27;</span> )&#123;     <span class="hljs-comment">//多向选择</span><br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;上课&#x27;</span>);<br>  &#125;   <br>  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>( <span class="hljs-built_in">day</span> == <span class="hljs-string">&#x27;星期四&#x27;</span>)&#123;<br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;上自习&#x27;</span>);<br>  &#125;<br>  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>( <span class="hljs-built_in">day</span> == <span class="hljs-string">&#x27;星期日&#x27;</span>)&#123;<br>      <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;休息&#x27;</span>);<br>  &#125;<br>  <span class="hljs-keyword">else</span>&#123;<br><br>  &#125;<br></code></pre></td></tr></table></figure>
<h5 id="if判断中的真假值"><a href="#if判断中的真假值" class="headerlink" title="if判断中的真假值"></a><strong>if判断中的真假值</strong></h5>false、0、‘’、undefined、null、NaN 六个值在if判断中为假值，其余未声明的均为真值  <figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-regexp">//</span>真假值(<span class="hljs-literal">false</span> <span class="hljs-number">0</span> <span class="hljs-string">&#x27;&#x27;</span> <span class="hljs-literal">undefined</span> <span class="hljs-literal">null</span> <span class="hljs-literal">NaN</span>)<br><span class="hljs-regexp">//</span>一个值也可以做判断，<span class="hljs-literal">false</span> <span class="hljs-number">0</span> <span class="hljs-string">&#x27;&#x27;</span> <span class="hljs-literal">undefined</span> <span class="hljs-literal">null</span> <span class="hljs-literal">NaN</span> 这<span class="hljs-number">6</span>个值返回假，其他值都返回真<br>   <span class="hljs-keyword">if</span>( [] == <span class="hljs-literal">true</span> )&#123;   [] -&gt; <span class="hljs-number">0</span>    <span class="hljs-literal">true</span> -&gt; <span class="hljs-number">1</span>   <span class="hljs-number">0</span> == <span class="hljs-number">1</span>  <span class="hljs-literal">false</span><br>       console.log(<span class="hljs-string">&#x27;if执行&#x27;</span>);<br>   &#125;<br>   <span class="hljs-keyword">if</span>( [] )&#123;    [] -&gt; 不在<span class="hljs-number">6</span>个假值内  -&gt;  <span class="hljs-literal">true</span><br>       console.log(<span class="hljs-string">&#x27;if执行&#x27;</span>);<br>   &#125;<br></code></pre></td></tr></table></figure>
<h4 id="emsp-2-switch判断"><a href="#emsp-2-switch判断" class="headerlink" title="&emsp;2.switch判断"></a>&emsp;2.switch判断</h4>语法格式：<figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">var</span> <span class="hljs-built_in">day</span> = <span class="hljs-string">&#x27;星期日&#x27;</span>;<br><br>   switch( <span class="hljs-built_in">day</span> )&#123;<br>       case <span class="hljs-string">&#x27;星期日&#x27;</span>:<br>           <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;执行1&#x27;</span>);<br>           <span class="hljs-keyword">break</span>;<br>       case <span class="hljs-string">&#x27;星期一&#x27;</span>:<br>           <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;执行2&#x27;</span>);<br>           <span class="hljs-keyword">break</span>;<br>       default:<br>           <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-string">&#x27;执行3&#x27;</span>);<br>   &#125;<br></code></pre></td></tr></table></figure></li>
</ol>
<p><strong>注意：switch语句中的判断是严格判断，即运用 <code>===</code>进行判断</strong></p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs stylus"><span class="hljs-function"><span class="hljs-title">switch</span><span class="hljs-params">(<span class="hljs-number">1</span>)</span></span>&#123;<br>        case <span class="hljs-string">&#x27;1&#x27;</span>:<br>            cccc  <span class="hljs-comment">//不执行</span><br>    &#125;<br></code></pre></td></tr></table></figure>
<h3 id="循环结构"><a href="#循环结构" class="headerlink" title="循环结构"></a>循环结构</h3><p>循环结构的目的就是为了节省重复代码的操作</p>
<h4 id="emsp-while循环"><a href="#emsp-while循环" class="headerlink" title="&emsp;while循环"></a>&emsp;while循环</h4><p>四个步骤：</p>
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs markdown"><span class="hljs-bullet">1.</span> 定义变量  <br><span class="hljs-bullet">2.</span> 条件判断(条件不满足的时候，循环结束)  <br><span class="hljs-bullet">3.</span> 3.执行语句  <br><span class="hljs-bullet">4.</span> 4.自增或自减<br></code></pre></td></tr></table></figure>

<p>代码结构：</p>
<figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">var</span> <span class="hljs-built_in">count</span> = <span class="hljs-number">0</span>;<br><br><span class="hljs-keyword">while</span>( <span class="hljs-built_in">count</span> &lt; <span class="hljs-number">5</span> )&#123;<br><br>	<span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>( <span class="hljs-number">123</span> );<br><br>	<span class="hljs-built_in">count</span>++;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>不要写出死循环，要写循环结束的条件逻辑</strong></p>
<h4 id="emsp-for循环"><a href="#emsp-for循环" class="headerlink" title="&emsp;for循环"></a>&emsp;for循环</h4><p>四个执行步骤 ( while和for可以互相转换的，等价的 )</p>
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs markdown"><span class="hljs-bullet">1.</span> 定义变量   <br><span class="hljs-bullet">2.</span> 条件判断(条件不满足的时候，循环结束)  <br><span class="hljs-bullet">3.</span> 执行语句  <br>4.自增或自减<br></code></pre></td></tr></table></figure>

<p>代码结构：</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs abnf">for(var i<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;i&lt;5;i++)&#123;</span><br>       console.log(i)<span class="hljs-comment">;</span><br>   &#125;<br></code></pre></td></tr></table></figure>

<p>for循环可以进行嵌套</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-built_in">for</span>(var i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">3</span>;i++)&#123;<br>		<span class="hljs-built_in">for</span>(var j=<span class="hljs-number">0</span>;j&lt;<span class="hljs-number">3</span>;j++)&#123;<br>			console<span class="hljs-selector-class">.log</span>(i , j);<br>		&#125;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h5 id="emsp-关于for循环的循环跳出"><a href="#emsp-关于for循环的循环跳出" class="headerlink" title="&emsp;关于for循环的循环跳出"></a>&emsp;关于for循环的循环跳出</h5><p>循环跳出可以使用两个语句，break和continue，下面简单讲一下两者的不同。</p>
<h6 id="emsp-emsp-break语句"><a href="#emsp-emsp-break语句" class="headerlink" title="&emsp;&emsp;break语句"></a>&emsp;&emsp;break语句</h6><p>break语句使用后程序会跳出整个循环</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs abnf">for(var i<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;i&lt;5;i++)&#123;</span><br>        console.log(i)<span class="hljs-comment">;</span><br>        if(i <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>)&#123;<br>            break<span class="hljs-comment">;</span><br>        &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="/images/break%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF.png" srcset="/img/loading.gif" lazyload alt="i = 2时跳出循环，故只打印 0 1 2 "><br>如果把if和console.log换一下位置</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs abnf">for(var i<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;i&lt;5;i++)&#123;</span><br>            if(i <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>)&#123;<br>                            break<span class="hljs-comment">;</span><br>                        &#125;<br>            console.log(i)<span class="hljs-comment">;</span><br>        &#125;<br></code></pre></td></tr></table></figure>

<p><img src="/images/break%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF_2.png" srcset="/img/loading.gif" lazyload></p>
<h6 id="emsp-emsp-continue语句"><a href="#emsp-emsp-continue语句" class="headerlink" title="&emsp;&emsp;continue语句"></a>&emsp;&emsp;continue语句</h6><p>continue语句使用后只会跳出当前循环</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs abnf">for(var i<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;i&lt;5;i++)&#123;</span><br>		if(i <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>)&#123;<br>			continue<span class="hljs-comment">;</span><br>		&#125;<br>		console.log(i)<span class="hljs-comment">;</span><br>   &#125;<br></code></pre></td></tr></table></figure>

<p><img src="/images/continue%E8%B7%B3%E5%87%BA.png" srcset="/img/loading.gif" lazyload alt="i = 2时跳出当前循环，循环继续，故打印 0 1 3 4"></p>
<p><strong>注意：嵌套循环的break默认跳出当前的循环，而不会跳出最外层循环</strong></p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs abnf">for(var i<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;i&lt;5;i++)&#123;</span><br>		for(var j<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-comment">;j&lt;3;j++)&#123;</span><br>			if( j <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>)&#123;<br>				console.log(j)<span class="hljs-comment">;</span><br>				break<span class="hljs-comment">;</span><br>			&#125;<br>		&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="/images/break%E8%B7%B3%E5%87%BA%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF.png" srcset="/img/loading.gif" lazyload alt="内层循环跳出，只执行外层循环"><br>那么如果想要退出最外层循环，那应该怎么办呢</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs scss">outer : <span class="hljs-built_in">for</span>(var i=<span class="hljs-number">0</span>;<span class="hljs-selector-tag">i</span>&lt;<span class="hljs-number">5</span>;<span class="hljs-selector-tag">i</span>++)&#123;   <span class="hljs-comment">//跳出到指定的某一层</span><br>					<span class="hljs-built_in">for</span>(var j=<span class="hljs-number">0</span>;j&lt;<span class="hljs-number">3</span>;j++)&#123;<br>						<span class="hljs-built_in">if</span>( j == <span class="hljs-number">1</span>)&#123;<br>							console<span class="hljs-selector-class">.log</span>(j);<br>							break outer;<br>						&#125;<br>					&#125;<br>				&#125;<br></code></pre></td></tr></table></figure>

<h4 id="emsp-do-while循环"><a href="#emsp-do-while循环" class="headerlink" title="&emsp;do-while循环"></a>&emsp;do-while循环</h4><p>和while不同之处:执行循环之前必定执行一次。</p>
<figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">do</span>&#123;<br>        <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(i);<br>        i++;<br>    &#125;<br>    <span class="hljs-keyword">while</span>(i&lt;<span class="hljs-number">5</span>);<br></code></pre></td></tr></table></figure>

<hr>
<hr>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/study/" class="category-chain-item">study</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaScript/">#JavaScript</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>初入JS（三）</div>
      <div>https://moewang0321.github.io/2019/08/06/2019-08-06-初入js（三）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Moe Wang</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2019年8月6日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2019/08/07/2019-08-07-JS%E5%87%BD%E6%95%B0%EF%BC%88%E4%B8%80%EF%BC%89/" title="JS函数">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">JS函数</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2019/08/06/2019-08-06-DOM/" title="DOM">
                        <span class="hidden-mobile">DOM</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
    <div id="giscus" class="giscus"></div>
    <script type="text/javascript">
      Fluid.utils.loadComments('#giscus', function() {
        var options = {"repo":"moewang0321/moewang0321.github.io","repo-id":"MDEwOlJlcG9zaXRvcnkyMDEyNDI0NTY=","category":"Show and tell","category-id":"DIC_kwDOC_63WM4CUMsH","theme-light":"light_protanopia","theme-dark":"dark_dimmed","mapping":"pathname","reactions-enabled":1,"emit-metadata":1,"input-position":"top","loading":"lazy","lang":"zh-CN"};
        var attributes = {};
        for (let option in options) {
          if (!option.startsWith('theme-')) {
            var key = option.startsWith('data-') ? option : 'data-' + option;
            attributes[key] = options[option];
          }
        }
        var light = 'light_protanopia';
        var dark = 'dark_dimmed';
        window.GiscusThemeLight = light;
        window.GiscusThemeDark = dark;
        attributes['data-theme'] = document.documentElement.getAttribute('data-user-color-scheme') === 'dark' ? dark : light;
        for (let attribute in attributes) {
          var value = attributes[attribute];
          if (value === undefined || value === null || value === '') {
            delete attributes[attribute];
          }
        }
        var s = document.createElement('script');
        s.setAttribute('src', 'https://giscus.app/client.js');
        s.setAttribute('crossorigin', 'anonymous');
        for (let attribute in attributes) {
          s.setAttribute(attribute, attributes[attribute]);
        }
        var ss = document.getElementsByTagName('script');
        var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
        e.parentNode.insertBefore(s, e.nextSibling);
      });
    </script>
    <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
